package com.lxj.group.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxj.group.mapper.GroupMapper;
import com.lxj.group.service.GroupService;
import com.lxj.utils.common.CommonResult;
import com.lxj.utils.entity.Group;
import com.lxj.utils.entity.RemoveRecord;
import com.lxj.utils.entity.TransferRecord;
import com.lxj.utils.entity.User;
import com.lxj.utils.vo.UserGroupRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    private GroupMapper groupMapper;
    @Override
    public CommonResult findStudent(Integer id) {
        List<User> list = groupMapper.findStudent(id);
        return CommonResult.success(list);
    }

    @Override
    public CommonResult findAll(Group group) {
        PageHelper.startPage(group.getPageNum(), group.getPageSize());
        PageInfo<Group> info = new PageInfo<>(this.groupMapper.findAll(group));
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", info.getList());
        map.put("number", info.getTotal());
        return CommonResult.success(map);
    }

    @Override
    public CommonResult delete(Integer[] id) {
        deleteGroupFromUser(id);
        deleteGroupFromUserGroup(id);
        int number = groupMapper.delete(id);
        if (number != 0) {
            return CommonResult.success(number);
        }
        return CommonResult.failed("删除失败");
    }
    public void deleteGroupFromUser(Integer[] id) {
        groupMapper.deleteGroupFromUser(id);
    }

    public void deleteGroupFromUserGroup(Integer[] id) {
        groupMapper.deleteGroupFromUserGroup(id);
    }
    @Override
    public CommonResult add(Group group) {
        int number = groupMapper.add(group);
        if (number != 0) {
            return CommonResult.success(number);
        }
        return CommonResult.failed("添加失败");
    }

    @Override
    public CommonResult findById(Integer id) {
        Group group = groupMapper.findById(id);
        return CommonResult.success(group);
    }

    @Override
    public CommonResult edit(Group group) {
        int number = groupMapper.edit(group);
        if (number != 0) {
            return CommonResult.success(number);
        }
        return CommonResult.failed("修改失败");
    }

    @Override
    public CommonResult findByGroupId(Integer id) {
        List<UserGroupRelation> list = groupMapper.findByGroupId(id);
        return CommonResult.success(list);
    }

    @Override
    public CommonResult addMember(Integer[] ids, Integer id) {
        updateGroupTip(ids, id);
        int result = groupMapper.addMember(ids, id);
        if (result == 0) {
            return CommonResult.failed();
        }
        addRecord(ids,id);
        return CommonResult.success(result);
    }

    /**
     * 添加调组记录
     * 1、根据用户id查询用户名
     * 2、根据小组id查询小组名
     * 3、保存数据
     * @param ids
     * @param id
     */
    private void addRecord(Integer[] ids,Integer id){
        List<String> list = groupMapper.findName(ids);
        String name = groupMapper.getGroupName(id);
        Assert.notNull(list,"用户名错误");
        Assert.notNull(name,"小组名错误");
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String format = dateFormat.format(date);
        int count = groupMapper.addRecord(list,name,format);
        if (count!=0){
            System.out.println(count);
        }
    }
    /**
     * 更改用户的小组标识状态，
     *
     * @param ids
     * @param id
     */
    public void updateGroupTip(Integer[] ids, Integer id) {
        try {
            groupMapper.updateGroupTip(ids, id);
        } catch (Exception e) {
            System.out.println("更改异常");
        }
    }
    private void deleteTransferRecord(Integer[] ids,Integer id){
        List<String> list = groupMapper.findName(ids);
        String name = groupMapper.getGroupName(id);
        Assert.notNull(list,"用户名错误");
        Assert.notNull(name,"小组名错误");
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String format = dateFormat.format(date);
        int count = groupMapper.deleteTransferRecord(list,name,format);
        if (count!=0){
            System.out.println(count);
        }
    }
    @Override
    @Transactional
    public CommonResult deleteMember(Integer id, Integer[] ids) {
        deleteGroupTip(ids, id);
        int result = groupMapper.deleteMember(ids, id);
        if (result == 0) {
            return CommonResult.failed();
        }
        deleteTransferRecord(ids,id);
        return CommonResult.success(result);
    }

    @Override
    public CommonResult selectMember(Integer id) {
        List<User> list = groupMapper.selectMember(id);
        return CommonResult.success(list);
    }

    @Override
    public CommonResult findAllRemoveRecord(RemoveRecord removeRecord) {
        List<RemoveRecord> list = groupMapper.findAllRemoveRecord(removeRecord);
        return CommonResult.success(list);
    }

    @Override
    public CommonResult deleteRecord(Integer[] id) {
        int number = groupMapper.deleteRecord(id);
        if (number != 0) {
            return CommonResult.success(number);
        }
        return CommonResult.failed("删除失败");
    }

    @Override
    public CommonResult findByUsername(String username) {
        List<User> list = groupMapper.findByUsername(username);
        Group group = groupMapper.findGroupName(list.get(0).getGroupTip());
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("name",group.getName());
        return CommonResult.success(map);
    }

    @Override
    public CommonResult findRecord(TransferRecord transferRecord) {
        PageHelper.startPage(transferRecord.getPageNum(),transferRecord.getPageSize());
        PageInfo<TransferRecord> info = new PageInfo<>(this.groupMapper.findRecord(transferRecord));
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",info.getList());
        map.put("number",info.getTotal());
        return  CommonResult.success(map);
    }

    public void deleteGroupTip(Integer[] ids, Integer id) {
        try {
            groupMapper.deleteGroupTip(ids, id);
        } catch (Exception e) {
            System.out.println("更改异常");
        }
    }
}
